<?php
global $_W, $_GPC;
$weid = $this->_weid;
$from_user = $this->_fromuser;
$setting = $this->getSetting();
$id = intval($_GPC['orderid']);
$status = trim($_GPC['status']);
$totalprice = floatval($_GPC['totalprice']);
$remark = trim($_GPC['remark']);

$orderstatus = array('cancel' => -1, 'confirm' => 1, 'finish' => 3, 'pay' => 2);

if (empty($orderstatus[$status])) {
    message('对不起，您没有该功能的操作权限!!');
}

if (empty($from_user)) {
    message('会话已过期，请重新发送关键字!');
}

$is_permission = false;
$tousers = explode(',', $setting['tpluser']);
if (in_array($from_user, $tousers)) {
    $is_permission = true;
}
if ($is_permission == false) {
    $accounts = pdo_fetchall("SELECT storeid FROM " . tablename($this->table_account) . " WHERE weid = :weid AND from_user=:from_user AND
 status=2 ORDER BY id DESC ", array(':weid' => $this->_weid, ':from_user' => $from_user));
    if ($accounts) {
        $arr = array();
        foreach ($accounts as $key => $val) {
            $arr[] = $val['storeid'];
        }
        $storeids = implode(',', $arr);
        $is_permission = true;
    }
}

if ($is_permission == false) {
    message('对不起，您没有该功能的操作权限!');
}

$order = pdo_fetch("SELECT * FROM " . tablename($this->table_order) . " WHERE id = :id ORDER BY id DESC LIMIT
         1", array(':id' => $id));
if (empty($order)) {
    message('订单不存在');
}

if ($orderstatus[$status] == 2) {
    pdo_update($this->table_order, array('ispay' => 1, 'totalprice' => $totalprice, 'remark' => $remark), array('id' => $order['id']));
    $paylog = pdo_fetch("SELECT * FROM " . tablename('core_paylog') . " WHERE tid=:tid AND uniacid=:uniacid AND status=0 AND module='weisrc_dish'
ORDER BY plid
DESC LIMIT 1", array(':tid' => $order['id'], ':uniacid' => $this->_weid));
    if (!empty($paylog)) {
        pdo_update('core_paylog', array('fee' => $_GPC['updateprice']), array('plid' => $paylog['plid']));
    }
} else {
    if ($orderstatus[$status] == -1) {
        if ($order['ispay'] == 1) {
            $coin = floatval($order['totalprice']);
            if ($order['paytype'] == 1 && $order['status'] != -1) {
                $this->setFansCoin($order['from_user'], $coin, "码上点餐单号{$order['ordersn']}退款");
            }
            pdo_update($this->table_order, array('status' => $orderstatus[$status], 'totalprice' => $totalprice, 'remark' => $remark), array('id' => $order['id']));
        }
    } else {
        pdo_update($this->table_order, array('status' => $orderstatus[$status], 'totalprice' => $totalprice, 'remark' => $remark), array('id' => $order['id']));
        $paylog = pdo_fetch("SELECT * FROM " . tablename('core_paylog') . " WHERE tid=:tid AND uniacid=:uniacid AND status=0 AND module='weisrc_dish'
ORDER BY plid
DESC LIMIT 1", array(':tid' => $order['id'], ':uniacid' => $this->_weid));
        if (!empty($paylog)) {
            pdo_update('core_paylog', array('fee' => $_GPC['updateprice']), array('plid' => $paylog['plid']));
        }
    }
}

if ($orderstatus[$status] == 3) {
    $this->updateFansData($order['from_user']);
    $this->updateFansFirstStore($order['from_user'], $order['storeid']);
    if ($order['isfinish'] == 0) {
        //计算积分
        $this->setOrderCredit($order['id']);
        pdo_update($this->table_order, array('isfinish' => 1), array('id' => $id));
        $fans = pdo_fetch("SELECT * FROM " . tablename($this->table_fans) . " WHERE weid = :weid  AND from_user=:from_user ORDER BY `id` DESC limit 1", array(':weid' => $weid, ':from_user' => $order['from_user']));
        pdo_update($this->table_fans, array('paytime' => TIMESTAMP), array('id' => $fans['id']));
        if ($order['dining_mode'] == 1) {
            pdo_update($this->table_tables, array('status' => 0), array('id' => $order['tables']));
        }
        $this->set_commission($order['id']);
    }
}

$store = pdo_fetch("SELECT * FROM " . tablename($this->table_stores) . " WHERE id=:id LIMIT 1", array(':id' => $order['storeid']));
if ($this->_accountlevel == 4) {
    $order = pdo_fetch("SELECT * FROM " . tablename($this->table_order) . " WHERE id = :id ORDER BY id DESC LIMIT
         1", array(':id' => $id));
    $this->sendOrderNotice($order, $store, $setting);
}

if ($status == 1 || $status == 3) {
    message('操作成功！', $this->createMobileUrl('AdminOrder', array('status' => $status), true), 'success');
}
message('操作成功！', $this->createMobileUrl('AdminOrder', array(), true), 'success');